﻿using Commen;
using DAL;
using DbEntity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AyaWo.SpiderService
{
    public partial class AddWebSite : Form
    {
        public AddWebSite()
        {
            InitializeComponent();
            txtinurl.Text = "http://www.chinaz.com/";
            txtlist.Text = "//ul[@class='imgTxtBar clearfix imgTxtBar-b aTabActive']//li";
            txtadd.Text = ".//a[@href]";
            txttitle.Text = "//h5[@class=\"w490\"]";
            txtContent.Text = "//div[class=\"txtBox w490\"]";
        }



        public AddWebSite(string id)
        {
            InitializeComponent();
        }
        string cont = string.Empty;
        /// <summary>
        /// 入口网址校验
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            cont = string.Empty;
            var inurl = txtinurl.Text.Trim();
            string typ = comboBox1.Text;
            try
            {
                cont = HtmlHelper.GetHtmlContent(inurl, typ);
                if (cont.NotNullOrWhiteSpace())
                {
                    setText(cont);
                    MessageBox.Show("网址没有问题啦...你看下面都有内容");
                }
                else
                {
                    MessageBox.Show("网址问题啦...你看下面没有内容");
                }
            }
            catch
            {
                MessageBox.Show("网址有问题啦...");
            }
        }

        HtmlAgilityPack.HtmlDocument doc = null;
        HtmlAgilityPack.HtmlNodeCollection hac = null;
        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (cont.IsNullOrWhiteSpace())
                return;
            doc = null;
            //加载源代码，获取文档对象
            doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(cont);
            string list = txtlist.Text;
            if (list.IsNullOrWhiteSpace())
            {
                MessageBox.Show("列表表达式不能为空哦.....");
                return;
            }
            hac = doc.DocumentNode.SelectNodes(list);
            if (hac == null)
            {
                MessageBox.Show("列表值为空.....");
                return;
            }
            setText(hac.FirstOrDefault().InnerHtml);
        }
        string contenturl = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            contenturl = null;
            var add = txtadd.Text;
            if (add.IsNullOrWhiteSpace())
            {
                MessageBox.Show("地址值不能为空.....");
                return;
            }
            if (hac == null)
                return;
            var hadd = hac.FirstOrDefault().SelectSingleNode(add);
            var ttyy = hadd.Attributes["href"];
            if (hadd == null)
            {
                MessageBox.Show("列表值为空.....");
                return;
            }

            contenturl = ttyy.Value;
            if (contenturl.IsNullOrWhiteSpace())
            {
                MessageBox.Show("获取的地址为空，请重新调整....");
                return;
            }
            setText(contenturl);
        }

        HtmlAgilityPack.HtmlDocument docContents = null;
        /// <summary>
        /// 标题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (contenturl.IsNullOrWhiteSpace())
                return;
            string typ = comboBox1.Text;
            try
            {
                docContents = null;
                string Contents = HtmlHelper.GetHtmlContent("http:" + contenturl, typ);
                if (Contents.NotNullOrWhiteSpace())
                {
                    docContents = new HtmlAgilityPack.HtmlDocument();
                    docContents.LoadHtml(cont);

                    string txtTitle = txttitle.Text;
                    if (txtTitle.IsNullOrWhiteSpace())
                    {
                        MessageBox.Show("标题值不能为空...");
                        return;
                    }
                    if (docContents != null)
                    {
                        var title = docContents.DocumentNode.SelectSingleNode(txtTitle);
                        if (title != null)
                        {
                            setText(title.InnerText);
                        }
                    }
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 来源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 作者
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///  发布时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            if (docContents != null)
            {
                string txtConten = txtContent.Text;
                var content = docContents.DocumentNode.SelectSingleNode(txtConten);
                if (content != null)
                {
                    setText(content.InnerHtml);
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            spcontent sp = Getspcontent();
            spcontentService.InsertContent(sp);
        }

        /// <summary>
        /// 草稿
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            //txtauto.Text = txtContent.Text = txtdate.Text = txtinurl.Text = txtlist.Text = txtsource.Text = txttitle.Text = txturl.Text = txtwebName.Text = null;
            spcontent sp = Getspcontent(true);
            spcontentService.InsertContent(sp);


        }


        public string getText()
        {
            return textBox1.Text;
        }

        public void setText(string str = "")
        {
            textBox1.Text = str;
        }

        public spcontent Getspcontent(bool caogao=false)
        {
            spcontent sp = new spcontent()
            {
                ID = CommonHelper.CreateNo(),
                Autos = txtauto.Text,
                BuileDate = txtdate.Text,
                Content = txtContent.Text,
                InUrl = txtinurl.Text,
                Title = txttitle.Text,
                list = txtlist.Text,
                Source = txtsource.Text,
                WebName = txtwebName.Text,
                WebUrl = txturl.Text,
                addr = txtadd.Text,
                typ = comboBox1.Text
            };

            sp.CiShu = yeshu.Text.ToInt();
            if (caogao)
                sp.caogao = 1;
            else
                sp.caogao = 0;
            return sp;
        }

        private void AddWebSite_FormClosed(object sender, FormClosedEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
